package Dao;

import Entity.Timetable;
import Mybatis.Mybatis;
import org.apache.ibatis.session.SqlSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TimetableDao implements TimetableDaoImpl {
    public List<Timetable> findAllTimetable() {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.TimetableMapper.findAllTimetable";
        List<Timetable> timetables = session.selectList(statement);
        session.close();
        return timetables;
    }
    public Timetable findById(int id) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.TimetableMapper.findById";
        Timetable timetable = session.selectOne(statement, id);
        session.close();
        return timetable;
    }
    public List<Timetable> findByRoomid(String roomid) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.TimetableMapper.findByRoomid";
        List<Timetable> timetables = session.selectList(statement, roomid);
        session.close();
        return timetables;
    }
    public List<Timetable> findByTime(int week, int day, int section) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("week", week);
        map.put("day", day);
        map.put("section", section);
        String statement = "Mapper.TimetableMapper.findByTime";
        List<Timetable> timetables = session.selectList(statement, map);
        session.close();
        return timetables;
    }
    public List<Timetable> findByTimeA(String roomid, int week, int day, int section) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        Timetable timetable = new Timetable(roomid, week, week, day, section);
        String statement = "Mapper.TimetableMapper.findByTimeA";
        List<Timetable> timetables = session.selectList(statement, timetable);
        session.close();
        return timetables;
    }
    public boolean addTimetable(Timetable timetable) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.TimetableMapper.addTimetable";
        int count = session.insert(statement, timetable);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean updateTimetable(Timetable timetable) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.TimetableMapper.updateTimetable";
        int count = session.update(statement, timetable);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }
    public boolean deleteTimetable(Timetable timetable) {
        Mybatis mybatis = new Mybatis();
        SqlSession session = mybatis.getSqlSession();

        String statement = "Mapper.TimetableMapper.deleteTimetable";
        int count = session.delete(statement, timetable);
        session.commit();
        session.close();
        if(count == 0) {
            return false;
        } else {
            return true;
        }
    }

    // ����
    public static void main(String[]args) {
        TimetableDao test = new TimetableDao();

        Timetable timetable = new Timetable("1012", 5, 12, 3, 4);
        test.addTimetable(timetable);
        timetable.setId(6);
        timetable.setSection(10);
        test.updateTimetable(timetable);
        test.deleteTimetable(timetable);
        List<Timetable> timetables = test.findAllTimetable();
        for(int i = 0; i < timetables.size(); i++) {
            System.out.println(timetables.get(i).getId()+" "+timetables.get(i).getRoomid()+" "+
                    timetables.get(i).getBegweek()+" "+timetables.get(i).getEndweek());
        }

        /*
        Timetable timetable = test.findById(3);
        System.out.println(timetable.getId()+" "+timetable.getRoomid());
         */
        /*
        List<Timetable> timetables = test.findByRoomid("1013");
        for(int i = 0; i < timetables.size(); i++) {
            System.out.println(timetables.get(i).getId()+" "+timetables.get(i).getRoomid()+" "+
                    timetables.get(i).getBegweek()+" "+timetables.get(i).getEndweek());
        }
         */
        /*
        List<Timetable> timetables = test.findByTime(10, 3, 2);
        for(int i = 0; i < timetables.size(); i++) {
            System.out.println(timetables.get(i).getId()+" "+timetables.get(i).getRoomid()+" "+
                    timetables.get(i).getBegweek()+" "+timetables.get(i).getEndweek());
        }
         */
        /*
        List<Timetable> timetables = test.findByTimeA("1003", 7, 3, 2);
        for(int i = 0; i < timetables.size(); i++) {
            System.out.println(timetables.get(i).getId()+" "+timetables.get(i).getRoomid()+" "+
                    timetables.get(i).getBegweek()+" "+timetables.get(i).getEndweek());
        }
         */
    }
}
